﻿using System;
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using KLS.PPDS.ApplicationService.Manager;

namespace KLS.PPDS.Web.Controllers.Article
{
    public class ImageActionResult : ActionResult
    {
        public ImageActionResult() { }
        public Image Image { get; set; } 
        public ImageFormat ImageFormat { get; set; }
        
        public string FullFileName { get; set; }

        public void SetFile() 
        {
            if (String.IsNullOrEmpty(this.FullFileName).Equals(false) && File.Exists(this.FullFileName).Equals(true)) 
            {
                Image = System.Drawing.Image.FromFile(FullFileName);

            }
        }

        public bool IsMathced 
        {
            get 
            {
                var result = false;
                if (string.IsNullOrEmpty(FullFileName).Equals(false)) 
                {
                    var ext = Path.GetExtension(FullFileName);
                    if (ext.ToLower().Equals(".bmp").Equals(true)) 
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".gif").Equals(true))
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".icon").Equals(true))
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".jpeg").Equals(true))
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".jpg").Equals(true))
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".png").Equals(true))
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".tif").Equals(true))
                    {
                        result = true;
                    }
                    else if (ext.ToLower().Equals(".wmf").Equals(true))
                    {
                        result = true;
                    }

                }

                return result;
            }
        }


        public override void ExecuteResult(ControllerContext context)
        {
                // verify properties 
            if (Image == null) 
            { 
                throw new ArgumentNullException("Image"); 
            } 
            //if (ImageFormat == null) 
            //{ 
            //    throw new ArgumentNullException("ImageFormat"); 
            //}
            // output 
            context.HttpContext.Response.Clear();
            context.HttpContext.Response.ContentType = UploadFileTypeManager.GetMimeTypeFromExtension(this.FullFileName);


         


            Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);
            context.HttpContext.Response.Flush();

            try { context.HttpContext.Response.End(); }
            catch { }
 
        } 
    }
}
